<?php
namespace App\Repositories;

use App\Models\Cimo\UserResumeProject;

class UserResumeProjestRepository
{
    use BaseRepository;
    
    private $model;
    
    public function __construct(UserResumeProject $userResumeProject)
    {
        $this->model = $userResumeProject;
    }
    
    /**
     * store.
     *
     * @param array $input
     */
    public function store($input)
    {
        return $this->savePost($this->model, $input);
    }
    
    /**
     * save.
     *
     * @param Request $input 输入
     */
    public function savePost($events, $input)
    {
        $events->fill($input);
        $events->save();
       // $events = self::_output($events);
        return $events;
    }
    
    /**
     * 更新
     * @param int $id
     * @param $input
     * @return \App\Models\AccountEvents
     */
    public function update($id, $input)
    {
        $model = $this->model->find($id);

        return $this->savePost($model, $input);
    }

    /**
     * Get Model by id.
     *
     * @param int $id
     *
     * @return App\Models\Model
     */
    public function getById($id,$with = '')
    {
        if($with)
            $data = $this->model->with($with)->find($id);
        else
            $data = $this->model->find($id);
        self::_output($data);
        return $data;
    }

    /**
     * 输出显示处理
     * @param mixed $data
     * @return bool
     */
    private static function _output(&$data)
    {
        if (!$data) return FALSE;
        if (is_numeric($data->start_at))
            $data->start_at = date('Y/m', $data->start_at);
        if (is_numeric($data->end_at))
            $data->end_at = date('Y/m', $data->end_at);
        return TRUE;
    }

    /**
     * 根据主键+用户编号查找，校验用户权限
     * @param $id
     * @param $uid
     * @return mixed
     */
    public function findWithUid($id, $uid)
    {
        $data = $this->model->join('user_resumes', 'user_resumes.id', '=', 'user_resume_projects.resume_id')
            ->where('user_resumes.user_id', '=', $uid)->find($id);
        self::_output($data);
        return $data;
    }
}


